﻿using Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 纺织贸易管理系统;

namespace 后整理管理系统.基本资料
{
    public partial class 新增原料 : Sunny.UI.UIForm
    {
        public RawMaterial Raw { get; set; }
        public FormUseful Useful { get; set; }
        public 新增原料()
        {
            InitializeComponent();
        }

        private void 新增原料_Load(object sender, EventArgs e)
        {
            txtpingming.Text = Raw.ProductName;
            txtguige.Text = Raw.Specifications;
            txtmenfu.Text = Raw.Width;
            txtkezhong.Text = Raw.Weight;
            txtyanse.Text = Raw.Color;
            txtbianma.Text = Raw.AuxiliaryEncoding;
            txtdanwei.Text = Raw.Unit;
            txtstock.Text = Raw.InventoryQuantity.ToString ();
            cmnisstock.Text = Raw.CalculateInventory;
            //txtalters.Text = Raw.InventoryAlerts.ToString ();
            if (this.Useful == FormUseful.修改)
            {
                txtNum.Text = Raw.Num;
            }
            else
            {
                CreateNewNum();
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            Raw.AuxiliaryEncoding = txtbianma.Text;
            Raw.CalculateInventory = cmnisstock.Text;
            Raw.Color = txtyanse.Text;
            Raw.InventoryQuantity = txtstock.Text.ToDecimal();
            Raw.ProductName = txtpingming.Text;
            Raw.Specifications = txtguige.Text;
            Raw.Unit = txtdanwei.Text;
            Raw.Weight = txtkezhong.Text;
            Raw.Width = txtmenfu.Text;
            Raw.Num = txtNum.Text;
            //Raw.InventoryAlerts=txtalters.Text .ToDecimal();
            if(Useful==FormUseful.新增 )
            {
                Connect.CreatConnect().Insert(Raw);
            }
            else
            {
                Connect.GetSugar().Updateable(Raw).ExecuteCommand();
            }
            Useful = FormUseful.新增;
            txtpingming.Text = string.Empty;
            txtguige.Text = string.Empty;
            txtmenfu.Text = string.Empty;
            txtkezhong.Text = string.Empty;
            txtyanse.Text = string.Empty;
            txtbianma.Text = string.Empty;
            txtdanwei.Text = string.Empty;
            txtstock.Text = "0";
            cmnisstock.Text = string.Empty;
            txtalters.Text = "0";
            CreateNewNum();
        }

        private void txtpingming_Leave(object sender, EventArgs e)
        {
            txtbianma.Text = Tools.GetPy.GetPingYingFirstLetter (txtpingming.Text);
        }
        private void CreateNewNum()
        {
            var dbcontext = Connect.GetSugar();
            var num = dbcontext.Queryable<OddNumbers>().First(x => x.Document_Type == "原料编号");
            if (num == null)
            {
                num = new OddNumbers()
                {
                    M_Date = DateTime.Now.Date,
                    Document_Type = "原料编号",
                    Odd_Numbers = 1,
                };
                Connect.CreatConnect().Insert<OddNumbers>(num);
                txtNum.Text = "00001";
            }
            else
            {
                num.Odd_Numbers++;
                dbcontext.Updateable(num).ExecuteCommand();
                txtNum.Text = string.Format("{0:00000}", num.Odd_Numbers);
            }
        }
    }
}
